package com.kongyz.consulconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 
 */
@RestController
public class CallHelloController {
    
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    
    @RequestMapping("/call")
    public String call(){
        ServiceInstance serviceInstance=loadBalancerClient.choose("consul-service-producer");
        System.out.println("服务地址："+serviceInstance.getUri());
        System.out.println("服务名称："+serviceInstance.getServiceId());
        
        String callServiceResult=new RestTemplate().getForObject(serviceInstance.getUri().toString()+"/consul/hello",String.class);
        System.out.println(callServiceResult);
        return callServiceResult;
    }
    
}
